import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
//生产者消费者模型
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2023-03-23
 * Time: 15:19
 */
public class ThreadDemo3 {
    public static void main(String[] args) {
        BlockingQueue<Integer> queue=new LinkedBlockingQueue();
        //消费者
        Thread t1=new Thread(()->{
            while(true){
                try {
                    int value=queue.take();
                    System.out.println("消费"+value);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        });
            t1.start();
            //生产者
        Thread t2=new Thread(()->{
            int value=0;
            while(true){
                try {
                    queue.put(value);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("生产"+value);
                value++;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t2.start();
    }
}
